# 引入模块
import time

from MY_ECShop.common.base import create_loading_browser, Base


class PageAddress(Base):
    # 定义熟悉
    url = "http://localhost:8080/ecshop/user.php"
    address = ("xpath", "//div[@class='userMenu']/a[4]")  # 收货地址按钮
    country = ("name", "country")  # 配送区域--国家
    province = ("name", "province")  # 配送区域--省
    city = ("name", "city")  # 配送区域--市
    district = ("name", "district")  # 配送区域--区县
    consignee_name = ("name", "consignee")  # 收货人姓名
    mailbox = ("name", "email")  # 电子邮件地址
    detailed_address = ("name", "address")  # 详细地址
    postal_code = ("name", "zipcode")  # 邮政编码
    tel = ("name", "tel")  # 电话
    phone = ("name", "mobile")  # 手机
    new_address = ("name", "submit")  # 新增收货地址
    delete_button = ("name", "button")  # 删除
    confir_change = ("name", "submit")  # 确认修改

    # 定义方法
    def page_addr_open_web(self):
        """
        打开网址
        :param url: 网址
        :return:
        """
        self.base_open_web(self.url)

    def page_add_click(self):
        """
        点击收货地址按钮
        :return:
        """
        self.base_click(self.address)

    def page_add_select_country(self, index, num):
        """
        选择配送区域--国家
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """
        self.base_many_select_option(self.country, index, num)

    def page_add_select_province(self, index, num):
        """
        选择配送区域--省
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """
        self.base_many_select_option(self.province, index, num)

    def page_add_select_city(self, index, num):
        """
        选择配送区域--市
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """

        self.base_many_select_option(self.city, index, num)

    def page_add_select_district(self, index, num):
        """
        选择配送区域--区县
        :param index: 多元素列表中定位的select元素的索引号
        :param num:  option索引号
        :return:
        """
        self.base_many_select_option(self.district, index, num)

    def page_add_name(self, num, message):
        """
        在输入框中输入收货人姓名
        :param num: 多元素的索引号
        :param message: 输入收货人姓名
        :return:
        """
        self.base_many_elements_send(self.consignee_name, num, message)

    def page_add_mail(self, num, message_input):
        """
        在输入框中输入电子邮件地址
        :param num: 多元素的索引号
        :param message: 输入电子邮件地址
        :return:
        """
        one_element = self.base_many_elements(self.mailbox, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_add_address(self, num, message):
        """
        在输入框中输入详细地址
        :param num: 多元素的索引号
        :param message: 输入详细地址
        :return:
        """
        self.base_many_elements_send(self.detailed_address, num, message)

    def page_add_postal_code(self, num, message):
        """
        在输入框中输入邮政编码
        :param num: 多元素的索引号
        :param message: 输入邮政编码
        :return:
        """
        self.base_many_elements_send(self.postal_code, num, message)

    def page_add_tel(self, num, message):
        """
        在输入框中输入电话
        :param num: 多元素的索引号
        :param message: 输入电话
        :return:
        """
        self.base_many_elements_send(self.tel, num, message)

    def page_add_phone(self, num, message):
        """
        在输入框中输入手机
        :param num: 多元素的索引号
        :param message: 输入手机
        :return:
        """
        self.base_many_elements_send(self.phone, num, message)

    def page_add_text(self, num):
        """
        获取名字文本值
        :param num:
        :return:
        """
        return self.base_many_get_value(self.consignee_name, num)

    def page_add_new_click(self, num):
        """
        点击新增收货地址
        :param num: 多元素的索引号
        :return:
        """
        self.base_many_elements_click(self.new_address, num)

    def page_del_accept_delete(self, num):
        """
        点击删除按钮后点击弹窗确认按钮
        :param num:索引号
        :return:
        """
        try:
            time.sleep(3)
            self.base_many_elements_click(self.delete_button, num)
            time.sleep(3)
            self.base_alert_accept()
        except:
            print("无法进行点击")
        else:
            return 1

    def page_del_esc_delete(self, num):
        """
        点击删除按钮后点击弹窗确认取消按钮
        :param num:索引号
        :return:
        """
        try:
            time.sleep(3)
            self.base_many_elements_click(self.delete_button, num)
            time.sleep(3)
            self.base_alert_dismiss()
        except:
            print("无法进行点击")
        else:
            return 1

    # ------------------------------------------------------------------------------------------------------------
    def page_modify_click(self):
        """
        点击收货地址按钮
        :return:
        """
        self.base_click(self.address)

    def page_modify_select_country(self, index, num):
        """
        选择配送区域--国家
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """
        self.base_many_select_option(self.country, index, num)

    def page_modify_select_province(self, index, num):
        """
        选择配送区域--省
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """
        self.base_many_select_option(self.province, index, num)

    def page_modify_select_city(self, index, num):
        """
        选择配送区域--市
        :param index: 多元素列表中定位的select元素的索引号
        :param num: option索引号
        :return:
        """

        self.base_many_select_option(self.city, index, num)

    def page_modify_select_district(self, index, num):
        """
        选择配送区域--区县
        :param index: 多元素列表中定位的select元素的索引号
        :param num:  option索引号
        :return:
        """
        self.base_many_select_option(self.district, index, num)

    def page_modify_name(self, num, message_input):
        """
        在输入框中输入收货人姓名
        :param num: 多元素的索引号
        :param message: 输入收货人姓名
        :return:
        """
        one_element = self.base_many_elements(self.consignee_name, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_mail(self, num, message_input):
        """
        在输入框中输入电子邮件地址
        :param num: 多元素的索引号
        :param message: 输入电子邮件地址
        :return:
        """
        one_element = self.base_many_elements(self.mailbox, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_address(self, num, message_input):
        """
        在输入框中输入详细地址
        :param num: 多元素的索引号
        :param message: 输入详细地址
        :return:
        """
        one_element = self.base_many_elements(self.detailed_address, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_postal_code(self, num, message_input):
        """
        在输入框中输入邮政编码
        :param num: 多元素的索引号
        :param message: 输入邮政编码
        :return:
        """
        one_element = self.base_many_elements(self.postal_code, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_tel(self, num, message_input):
        """
        在输入框中输入电话
        :param num: 多元素的索引号
        :param message: 输入电话
        :return:
        """
        one_element = self.base_many_elements(self.tel, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_phone(self, num, message_input):
        """
        在输入框中输入手机
        :param num: 多元素的索引号
        :param message: 输入手机
        :return:
        """
        one_element = self.base_many_elements(self.phone, num)
        self.base_element_clear(one_element)
        self.base_element_send_keys(one_element, message_input)

    def page_modify_new_click(self, num):
        """
        点击确认修改
        :param num: 多元素的索引号
        :return:
        """
        self.base_many_elements_click(self.new_address, num)

    def page_addr_quit_web(self):
        """
        关闭浏览器对象
        :return:
        """

        self.base_quit_web()


if __name__ == '__main__':
    driver = create_loading_browser("邓桠琳")  # 创建一个浏览器对象
    # 点击立即登录按钮
    # --------------------------------------------------------------------------------------------------
    addr1 = PageAddress(driver)  # 实例化一个ECshop的登录页面对象
    addr1.page_addr_open_web()
    time.sleep(3)
    addr1.page_add_click()  # 点击收货地址按钮
    time.sleep(2)
    # addr1.page_add_select_country(2, 1)  # 选择配送区域--国家
    # time.sleep(5)
    # addr1.page_add_select_province(2, 1)  # 选择配送区域--省
    # time.sleep(5)
    # addr1.page_add_select_city(2, 1)  # 选择配送区域--市
    # time.sleep(5)
    # addr1.page_add_select_district(2, 1)  # 选择配送区域--区县
    # addr1.page_add_name(2, "小张")  # 输入收货人姓名
    # addr1.page_add_mail(2, "2772662552@qq.com")  # 输入电子邮件地址
    # addr1.page_add_address(2, "新成国际")  # 输入详细地址
    # addr1.page_add_postal_code(2, "610000")  # 输入邮政编码
    # addr1.page_add_tel(2, "27869045")  # 输入电话
    # addr1.page_add_phone(2, "13456784567")  # 输入手机
    # time.sleep(5)
    # result = addr1.page_add_text(2)  # 获取地址姓名文本值
    # print(result)
    # time.sleep(5)
    # addr1.page_add_new_click(2)  # 点击添加新地址按钮

    # time.sleep(5)
    # result2 = addr1.page_del_accept_delete(0)  # 点击删除按钮后点击弹窗确认按钮
    # print(result2)
    result1 = addr1.page_del_esc_delete(0)  # 点击删除按钮后点击弹窗确认取消按钮
    print(result1)
    # --------------------------------------------------------------------------------
    # time.sleep(3)
    # addr1.page_modify_click()  # 点击收货地址按钮
    # time.sleep(2)
    # addr1.page_modify_select_country(2, 1)  # 选择修改配送区域--国家
    # time.sleep(5)
    # addr1.page_modify_select_province(2, 1)  # 选择修改配送区域--省
    # time.sleep(5)
    # addr1.page_modify_select_city(2, 1)  # 选择修改配送区域--市
    # time.sleep(5)
    # addr1.page_modify_select_district(2, 1)  # 选择修改配送区域--区县
    # addr1.page_modify_name(2, "小利")  # 修改输入收货人姓名
    # addr1.page_modify_mail(2, "2772662552@qq.com")  # 修改输入电子邮件地址
    # addr1.page_modify_address(2, "新成国际")  # 修改输入详细地址
    # addr1.page_modify_postal_code(2, "610000")  # 修改输入邮政编码
    # addr1.page_modify_tel(2, "27869045")  # 修改输入电话
    # addr1.page_modify_phone(2, "13456784567")  # 修改输入手机
    # time.sleep(5)
    # result = addr1.page_add_text(2)  # 获取地址姓名文本值
    # print(result)
    # addr1.page_modify_new_click(2)  # 点击确认修改地址按钮
    time.sleep(5)
    addr1.page_addr_quit_web()  # 关闭浏览器对象
